package net.starwifi.duobao.web;

import net.starwifi.duobao.SettingUtils;
import net.starwifi.duobao.service.SmsSendService;
import net.starwifi.duobao.utils.FreemarkerUtils;
import net.starwifi.duobao.utils.StringUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Administrator on 2016/1/29.
 */
@Controller("adminSmsController")
@RequestMapping("/sms")
public class SmsSendController extends BaseController {


    @Autowired
    SmsSendService smsSendService;
    /**
     * 添加
     */
    @RequestMapping(value = "/send", method = RequestMethod.GET)
    public String add(ModelMap model) {
        return "/sms/send";
    }

    /**
     * 保存
     */

    @RequestMapping(value = "/sendUpdate", method = RequestMethod.POST)
    public String save(String templateId,String content,String contentTwo,MultipartFile file, RedirectAttributes redirectAttributes) {
        String filePath=upload(file,"/jms/sms/");
        smsSendService.massSms(templateId,content,contentTwo,filePath);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:/sms/send";
    }


    public String upload(MultipartFile multipartFile,String uploadPath) {
        if (multipartFile == null) {
            return null;
        }
        try {
            String realPath=null;
            String uuid= StringUtils.getUUID();
            Map<String, Object> model = new HashMap<String, Object>();
            model.put("uuid",uuid);
            String path = FreemarkerUtils.process(uploadPath, model);
            String destPath = path + uuid + "." + FilenameUtils.getExtension(multipartFile.getOriginalFilename());
            File tempFile = new File(System.getProperty("java.io.tmpdir") + "/upload_" + UUID.randomUUID() + ".tmp");
            if (!tempFile.getParentFile().exists()) {
                tempFile.getParentFile().mkdirs();
            }
            multipartFile.transferTo(tempFile);
            try {
                 realPath= SettingUtils.get("uploadPath")+destPath;
                File destFile = new File(realPath);
                System.out.println("---------file:" + realPath);
                try {
                    FileUtils.moveFile(tempFile, destFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } finally {
                FileUtils.deleteQuietly(tempFile);
            }
            return SettingUtils.get("uploadPath")+destPath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
